//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI.WebControls;
using DotNetNuke.Modules.NavigationProvider;
using DotNetNuke.UI.WebControls;
namespace DotNetNuke.UI.Containers
{
	partial class DropDownActions : UI.Containers.ActionBase
	{
		private NavigationProvider m_objControl;
		private string m_strProviderName = "DNNDropDownNavigationProvider";
		public string ProviderName
		{
			get { return m_strProviderName; }
			set { }
		}
		public NavigationProvider Control
		{
			get { return m_objControl; }
		}
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			this.cmdGo.Attributes.Add("onclick", "if (cmdGo_OnClick(dnn.dom.getById('" + Control.NavigationControl.ClientID + "')) == false) return false;");
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdGo.Click += new System.Web.UI.ImageClickEventHandler(cmdGo_Click);

			try
			{
				BindDropDown();
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
			
		}
		private void cmdGo_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				DropDownList cboActions = (DropDownList)Control.NavigationControl;
				if (cboActions.SelectedIndex != -1)
				{
					ProcessAction(cboActions.SelectedItem.Value);
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		public void BindDropDown()
		{
			DNNNodeCollection objNodes;
			objNodes = Navigation.GetActionNodes(ActionRoot, this);
			foreach (DNNNode objNode in objNodes)
			{
				ProcessNodes(objNode);
			}
			Control.Bind(objNodes);
			this.Visible = DisplayControl(objNodes);
		}
		private void ProcessNodes(DNNNode objParent)
		{
			if (!String.IsNullOrEmpty(objParent.JSFunction))
			{
				DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(this.Page, "__dnn_CSAction_" + this.Control.NavigationControl.ClientID + "_" + objParent.ID, objParent.JSFunction, true);
			}
			objParent.ClickAction = eClickAction.None;
			foreach (DNNNode objNode in objParent.DNNNodes)
			{
				ProcessNodes(objNode);
			}
		}
		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit(e);
			m_objControl = NavigationProvider.Instance(this.ProviderName);
			Control.ControlID = "ctl" + this.ID;
			Control.Initialize();
			spActions.Controls.Add(Control.NavigationControl);
		}
	}
}
